package com.ihr360.payroll.controller.spreadsheet.validator;

import com.google.common.collect.Lists;
import com.ihr360.commons.context.session.Ihr360Session;
import com.ihr360.commons.context.session.Ihr360SessionContextHolder;
import com.ihr360.excel.specification.ColumnSpecification;
import com.ihr360.excel.specification.CommonSpecification;
import com.ihr360.excel.validation.SpreadSheetValidator;
import com.ihr360.excel.validation.annonation.SpreadSheetValid;
import com.ihr360.excel.vo.CustomSpreadSheetValidateResult;
import com.ihr360.excel.vo.SpreadSheetSpecification;
import com.ihr360.excel.vo.SpreadSheetValidatorRequest;
import com.ihr360.payroll.service.attendancereport.AttendanceBillService;
import com.ihr360.payroll.service.attendancereport.impl.AttendanceBillServiceImpl;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@SpreadSheetValid(code = "SalaryBill")
public class AttendanceBillImportValidator implements SpreadSheetValidator {

    @Autowired
    private AttendanceBillService attendanceBillService;

    @Override
    public CustomSpreadSheetValidateResult customValidate(SpreadSheetValidatorRequest request) {

        Ihr360Session session = Ihr360SessionContextHolder.getSession();
        if (session == null || session.getUserInfo() == null
                || session.getUserInfo().getCompanyId() == null
                || session.getUserInfo().getUserId() == null) {
            ValidationUtils.reject(null, "【考勤单据导入校验】请求参数无效");
        }

        Map<String, Object> extraParams = request.getExtraParams();
        if (extraParams == null) {
            ValidationUtils.reject(null, "【考勤单据导入校验】请求参数无效");
        }
        Long summarId = Long.parseLong(extraParams.get(AttendanceBillServiceImpl.SUMMARY_ID).toString());


        return attendanceBillService.validateExcelData(
                session.getUserInfo().getCompanyId(),
                session.getUserInfo().getUserId(),
                summarId,
                request);
    }

    @Override
    public SpreadSheetSpecification commonSpecification(SpreadSheetValidatorRequest request) {
        SpreadSheetSpecification spreadSheetSpecification = new SpreadSheetSpecification();
        CommonSpecification commonSpecification = CommonSpecification.createCommonSpecification(true);
        List<List<String>> requiredHeaders = Lists.newArrayList();
        ArrayList<String> staffNameColumn = Lists.newArrayList("姓名");
        requiredHeaders.add(staffNameColumn);
        ArrayList<String> mobileNoColumn = Lists.newArrayList("手机号");
        requiredHeaders.add(mobileNoColumn);
        commonSpecification.setHeaderColumnJudge(requiredHeaders);
        commonSpecification.setStaffNameAlias(staffNameColumn);
        commonSpecification.setMobileNoAlias(mobileNoColumn);
        spreadSheetSpecification.setCommonSpecification(commonSpecification);

        //必填项
        List<ColumnSpecification> columnSpecifications = new ArrayList<>();
        ColumnSpecification strNullAbleColumnSpe = ColumnSpecification.createCellSpecification(String.class, false);
        strNullAbleColumnSpe.addColumns("姓名");
        strNullAbleColumnSpe.addColumns("手机号");
        strNullAbleColumnSpe.addColumns("单据类型");
        ColumnSpecification dateNullAbleColumnSpe = ColumnSpecification.createCellSpecification(Date.class, false);
        dateNullAbleColumnSpe.addColumns("日期");
        ColumnSpecification doubleNullAbleColumnSpe = ColumnSpecification.createCellSpecification(Double.class, false);
        doubleNullAbleColumnSpe.addColumns("时长");

        ColumnSpecification strColumnSpe = ColumnSpecification.createCellSpecification(String.class, true);
        strColumnSpe.addColumns("事由");

        ColumnSpecification dateColumnSpe = ColumnSpecification.createCellSpecification(Date.class, true);
        dateColumnSpe.addColumns("申请时间");

        columnSpecifications.add(dateNullAbleColumnSpe);
        columnSpecifications.add(strNullAbleColumnSpe);
        columnSpecifications.add(doubleNullAbleColumnSpe);
        columnSpecifications.add(strColumnSpe);
        columnSpecifications.add(dateColumnSpe);
        spreadSheetSpecification.setColumnSpecifications(columnSpecifications);
        return spreadSheetSpecification;
    }

}
